1621C - Hidden Permutations - CodeForces Solution


dfs and similar interactive math *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define V vector
#define L long
#define LL long long
#define P pair
#define VI V<int>
#define VLL V<LL>
#define VVI V<VI>
#define VVLL V<VLL>
#define VB V<bool>
#define PII P<int, int> 
#define PLL P<LL, LL>

#define loop(i,a,b) for(int i=a; i<b; i++)
#define pb push_back
#define FF first
#define SS second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define input(v,n)  loop(i,0,n)  cin>>v[i];
#define output(v,n) loop(i,0,n) cout<<v[i]<<" ";
#define nl cout<<"\n"
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define print(a) cout<<a<<"\n"
#define el cout<<endl;

const LL MOD = 1e9 + 7;
const double EPS = 1e-6;

/********************************************************************/

int query(int n) {
    cout << "? " << n << endl;
    int x; 
    cin >> x;
    return x;
}


void solve(){
    int n;
    cin >> n;

    vector<int> ans(n+1, -1);

    for(int i=1; i<=n; i++)
        if(ans[i] == -1) {
            int start = query(i);
            int prev = start;
            while(1) {
                int x = query(i);
                ans[prev] = x;
                prev = x;
                if(x == start)
                    break;
            }
        }

    cout << "! ";
    for(int i=1; i<=n; i++)
        cout << ans[i] << " ";
    cout << endl;
}

/********************************************************************/


int32_t main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif  

    int t;
    t = 1;
    cin>>t;
    while(t--)
        solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification